<template>
	<el-dialog :visible.sync="options.showDialog" width="800px" class="evalDialog">
		<div class="search-header" style="padding: 20px 0 0;">
			<el-input placeholder="通过企业名称/岗位名称搜索（搜索对象为发才网内所有岗位)" :disabled="options.type == 'reshelf'" v-model="value"
				class="input-with-select">
				<el-button slot="append" icon="el-icon-search" @click="searchData"
					:disabled="options.type == 'reshelf'"></el-button>
			</el-input>
		</div>
		<div class="result-container">
			<el-table ref="multipleTable" border :data="tableData" height="400" style="width: 100%" @row-click="getname"
				@selection-change="handleSelectionChange">
				<!-- <el-table-column prop="shopname" label="门店名称" align="center"></el-table-column> -->
				<el-table-column prop="positionname" label="岗位名称" align="center"></el-table-column>
				<!-- <el-table-column prop="username" label="发布人" align="center"></el-table-column> -->
				<!-- <el-table-column align="center" type="selection" width="55"> </el-table-column> -->
			</el-table>
		</div><!-- 标记 -->
		<div class="form-data">
			<el-form ref="form" :model="form" :rules="rules" label-width="150px" style="overflow: hidden;">
				<!-- <el-form-item label="员工自入职奖励标准" style="float: left;" prop="cost">
					<el-input v-model="form.cost" placeholder="请输入奖励金额"></el-input>
				</el-form-item>
				<el-form-item label-width="20px" style="float: left;width: 150px;" prop="returnType">
					<el-select v-model="form.returnType" @change="selectedChange" placeholder="请选择奖励方式">
						<el-option label="每小时" value="1"></el-option>
						<el-option label="每月" value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="门店送入职奖励标准" style="float: left;" prop="cost">
					<el-input v-model="form.manageCost" placeholder="请输入奖励金额"></el-input>
				</el-form-item>
				<el-form-item label-width="20px" style="float: left;width: 150px;" prop="returnType">
					<el-select v-model="form.manageReturnType" @change="selectedChange" placeholder="请选择奖励方式">
						<el-option label="每小时" value="1"></el-option>
						<el-option label="每月" value="2"></el-option>
					</el-select>
				</el-form-item> -->

				<!-- 	<el-form-item label="发放备注" style="float: left;" prop="returnTime">
					<el-input v-model="form.returnTime" placeholder="请输入返费发放备注"></el-input>
				</el-form-item>
				
				<el-form-item label="奖励标准" style="float: left;" prop="rewardStandard">
					<el-input v-model="form.rewardStandard" placeholder="请输入返费发放备注"></el-input>
				</el-form-item> -->



			</el-form>
		</div>
		<!-- <div class="subBtn tc">
			<el-button type="primary" size="small" @click="onSubmit">确认发布</el-button>
		</div> -->
	</el-dialog>
</template>

<script>
	export default {
		props: {
			options: {
				type: Object,
				require: true
			}
		},
		data() {
			return {
				value: '', //搜索内容
				tableData: [],
				form: {
					returnTime: '', //返费时间
					rewardStandard: '' //奖励标准

				},
				rules: {
					rewardStandard: [{
						required: true,
						message: '奖励标准',
						trigger: 'blur'
					}, ],
					returnTime: [{
						required: true,
						message: '请选择返费发放时间',
						trigger: 'blur'
					}, ]
				},
				returnTimeList: [], //时间数组
			}
		},
		created() {
			this.returnTimeList = [];
			for (let i = 1; i < 32; i++) {
				this.returnTimeList.push('每月' + i + '号');
			}
			this.updatadata = this.$store.state.value;
		},

		methods: {

			//奖励方式绑定
			selectedChange(val) {
				console.log(val);
				this.form.returnType = val;
				this.form.manageReturnType = val;
			},
			/* 根据条件搜索数据 */
			searchData() {
				if (!this.value) {
					this.$message.warning('企业名称或者岗位名称不能为空！');
					return false;
				} else {
					this.$storesAxios.post('v1/shop/specialshop', {
						searchName: this.value,
					}).then(res => {
						console.log(res);
						if (res.data.code === 1) {
							let result = res.data.data;
							this.tableData = result;
						} else {
							this.$message.warning(res.data.message);
						}
					})
				}

			},
			toggleSelection(rows) {
				console.log(rows);
				if (rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, true);
					});
				} else {
					this.$refs.multipleTable.clearSelection();
				}
			},
			handleSelectionChange(val) {
				console.log(val);
				this.multipleSelection = val;
			},

			getname(row) {
				console.log(row);
				this.options.showDialog = false;
				this.$emit('handleChange', row.positionname)
			},
			onSubmit() {
				// var t = this
				// const loading = t.$loading({
				// 	lock: true,
				// 	text: '正在加载。。。',
				// 	spinner: 'el-icon-loading',
				// 	background: 'rgba(0, 0, 0, 0.7)'
				// });
				// setTimeout(() => {
				// 	loading.close();
				// }, 6000);


				if (this.tableData.length == 0) {
					this.$message.warning('请先查询上架岗位并勾选！');
					return false;
				}
				// if (this.multipleSelection.length == 0) {
				// 	this.$message.warning('请先勾选列表中至少一个岗位！');
				// 	return false;
				// }
				// if (  !this.form.returnTime || !this.form.rewardStandard) {
				// 	this.$message.warning('请将信息填写完整！');
				// 	return false;
				// }
				//修改职位信息




				//重新上架岗位
				if (this.options.type == 'reshelf') {
					let params = {
						delete: 1,
						ids: [],
					}
					Object.assign(params, this.form);
					this.multipleSelection.forEach(item => {
						params.ids.push(item.id);
					})
					console.log(params);
					this.$storesAxios.post('/v1/shop/updateAnnouncementPosition', params).then(res => {
						console.log(res);
						if (res.data.code === 1) {
							this.$message({
								type: 'success',
								message: '上架成功!',
							});
							this.clearData();
						} else {
							this.$message.warning(res.data.message);
						}
					})
				} else if (this.options.type == 'updateTheShelf') {
					let params = {
						delete: 1,
						ids: [],
					}
					Object.assign(params, this.form);
					this.multipleSelection.forEach(item => {
						params.ids.push(item.id);
					})
					this.$storesAxios.post('/v1/shop/updateAnnouncementPosition', params).then(res => {
						if (res.data.code === 1) {
							console.log(res.data.code);
							console.log(res.data.data + '数据');
							this.$message({
								type: 'success',
								message: '修改岗位成功!'
							});
							this.clearData();


							console.log('修改成功');
						} else {
							this.$message.warning(res.data.message);
						}
					})
				} else {
					//新增岗位
					let params = {
						userid: JSON.parse(localStorage.getItem('userinfo')).userid,
						userName: JSON.parse(localStorage.getItem('userinfo')).username,
						positionidList: []
					}
					Object.assign(params, this.form);
					this.multipleSelection.forEach(item => {
						params.positionidList.push(item.positionid);
					})
					this.$storesAxios.post('/v1/shop/insertPositionReturnCost', params).then(res => {
						console.log(res);
						if (res.data.code === 1) {
							this.$message({
								type: 'success',
								message: '添加岗位成功!'
							});
							this.clearData();
						} else {
							this.$message.warning(res.data.message);
						}
					})
				}

			},
			clearData() {
				this.tableData = [];
				this.multipleSelection = [];
				this.value = '';
				this.form = {
					returnTime: '', //返费时间
					rewardStandard: '' //奖励标准
				};
				this.$emit('closeDialog');
			}
		},
		watch: {
			'options.showDialog'() {
				if (this.options.showDialog && this.options.type == 'reshelf') {
					this.form = {
						// cost: this.options.userInfo.cost, //返费金额
						// manageCost: this.options.userInfo.manageCost, //返费金额
						// returnType: this.options.userInfo.returnType, //返费方式
						// manageReturnType: this.options.userInfo.returnType, //返费方式
						returnTime: this.options.userInfo.returnTime, //返费时间
						rewardStandard: this.options.userInfo.rewardStandard //奖励标准
					}
					this.options.userInfo.username = this.options.userInfo.hrname;
					this.tableData = [this.options.userInfo];
					this.multipleSelection = [this.options.userInfo];
					this.$nextTick(() => {
						this.toggleSelection(this.multipleSelection);
					})
				}
				if (this.options.showDialog && this.options.type == 'updateTheShelf') {
					this.form = {
						// cost: this.options.userInfo.cost, //返费金额
						// manageCost: this.options.userInfo.manageCost, //返费金额
						// returnType: this.options.userInfo.returnType, //返费方式
						// manageReturnType: this.options.userInfo.returnType, //返费方式
						returnTime: this.options.userInfo.returnTime, //返费时间
						rewardStandard: this.options.userInfo.rewardStandard //奖励标准
					}
					this.options.userInfo.username = this.options.userInfo.hrname;
					this.tableData = [this.options.userInfo];
					this.multipleSelection = [this.options.userInfo];
					this.$nextTick(() => {
						this.toggleSelection(this.multipleSelection);
					})
				}

				if (this.options.showDialog) {
					this.value = '';
				}
			}
		}
	}
</script>

<style scoped="scoped">
	.search-header,
	.form-data,
	.subBtn {
		margin: 0 auto;
		width: 550px;
		padding: 0 0 20px;
	}

	.result-container {
		width: 640px;
		margin: 0 auto;
	}
</style>
